/*
 * Copyright (C) 2021 The Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.oushangfeng.pinneddemo.custom.categorylist;

import com.oushangfeng.pinneddemo.ResourceTable;
import com.oushangfeng.pinneddemo.datainterface.CategoryItemBase;
import com.oushangfeng.pinneddemo.util.ToastUtil;

import ohos.agp.components.Component;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;
import ohos.media.image.common.Rect;
import ohos.media.image.common.Size;

import java.io.IOException;
import java.io.InputStream;

/**
 * SingleListItem
 *
 * @since 2021-03-27
 * Created by oubowu on 2021/3/27 10:29
 * SingleList item which display a left text and a right text in the list container item.
 */
public class SingleListItem implements CategoryItemBase {
    /**
     * SingleListItem type
     */
    public static final int ITEM_TYPE = 2;

    private final Context context;
    private String leftText;
    private String rightText;
    private ToastUtil toastUtil;
    /**
     * SingleListItem
     *
     * @param context
     * @param leftText
     * @param rightText
     */
    SingleListItem(Context context, String leftText, String rightText) {
        this.context = context;
        this.leftText = leftText;
        this.rightText = rightText;
        toastUtil = new ToastUtil(context);
    }

    /**
     * Set component content
     *
     * @param component the component to set
     */
    @Override
    public void bindComponent(Component component) {
        // item context
        DependentLayout dependentLayout = (DependentLayout) component.findComponentById(ResourceTable.Id_item_dl_all);
        Text contentText = (Text) component.findComponentById(ResourceTable.Id_item_content_text);
        contentText.setText(leftText);

        Image image = (Image) component.findComponentById(ResourceTable.Id_item_right_icon);
        PixelMap bitmap = getPixelMap(Integer.parseInt(rightText));
        image.setPixelMap(Integer.parseInt(rightText));
        int hi = bitmap.getImageInfo().size.height;
        int wi = bitmap.getImageInfo().size.width;
        int dlHeight = dependentLayout.getHeight();
        int bl = dlHeight / hi;
        image.setHeight(dlHeight);
        image.setWidth(bl * wi);
    }

    /**
     * Create component
     *
     * @return component
     */
    @Override
    public Component createComponent() {
        return LayoutScatter.getInstance(context).parse(
                ResourceTable.Layout_list_item, null, false);
    }

    /**
     * Get the item type of this category item
     *
     * @return category item type
     */
    @Override
    public int getItemType() {
        return ITEM_TYPE;
    }

    /**
     * getItem
     *
     * @return getItem
     */
    @Override
    public String getItem() {
        return leftText;
    }

    /**
     * setItem
     *
     * @param value
     */
    @Override
    public void setItem(String value) {
        leftText = value;
    }

    /**
     * getItemName
     *
     * @return getItemName
     */
    @Override
    public String getItemName() {
        return leftText;
    }

    /**
     * 通过资源ID获取位图对象
     *
     * @param resId
     * @return getPixelMap
     */
    private PixelMap getPixelMap(int resId) {
        PixelMap nuPm = null;
        InputStream drawableInputStream = null;
        try {
            drawableInputStream = context.getResourceManager().getResource(resId);
            ImageSource.SourceOptions sourceOptions = new ImageSource.SourceOptions();
            sourceOptions.formatHint = "image/jpg";
            ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
            decodingOptions.desiredSize = new Size(0, 0);
            decodingOptions.desiredRegion = new Rect(0, 0, 0, 0);
            decodingOptions.desiredPixelFormat = PixelFormat.ARGB_8888;
            ImageSource imageSource = ImageSource.create(drawableInputStream, null);
            PixelMap pixelMap = imageSource.createPixelmap(decodingOptions);
            return pixelMap;
        } catch (NotExistException | IOException e) {
            toastUtil.toast(context, "Exception");
        } finally {
            try {
                if (drawableInputStream != null) {
                    drawableInputStream.close();
                }
            } catch (IOException e) {
                toastUtil.toast(context, "FIN Exception");
            }
        }
        return nuPm;
    }
}
